package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.beans.user.LinkUser;
import com.hdax.dm.dto.ticketbuyerlistDto;
import com.hdax.dm.mappers.LinkuserMapper;
import com.hdax.dm.mapstucts.ticketbuyerlistDtoMapper;
import com.hdax.dm.service.LinkuserService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class LinkuserServiceImpl extends ServiceImpl<LinkuserMapper, LinkUser> implements LinkuserService {

    @Autowired
    private ticketbuyerlistDtoMapper ticketbuyerlistDtoMapper;

    @Override
    public CommonResponse<List<ticketbuyerlistDto>> linkUser(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId",id);
        List<LinkUser> lists = baseMapper.selectList(queryWrapper);
        List<ticketbuyerlistDto> dtos = ticketbuyerlistDtoMapper.toTargetList(lists);
        return ResponseUtil.returnSuccess(dtos);
    }

    @Override
    public CommonResponse validatebuyerexist(Map<String,Object> params) {
        String idCard = (String) params.get("idCard");

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("idCard",idCard);
        LinkUser linkUser = baseMapper.selectOne(queryWrapper);
        if (linkUser!=null){
            return ResponseUtil.returnErrorCode("1009");
        }
        return ResponseUtil.returnErrorCode("0000");
    }

    @Override
    public CommonResponse<String> addticketbuyer(Map<String, Object> params,Long id) {

        Long cardType = Long.parseLong((String) params.get("cardType"));
        String idCard = (String) params.get("idCard");
        String name = (String) params.get("name");

        LinkUser linkUser = new LinkUser();

        linkUser.setUserId(id);

        linkUser.setIdCard(idCard);
        linkUser.setName(name);
        linkUser.setCardType(cardType);

        baseMapper.insert(linkUser);

        return ResponseUtil.returnSuccess("添加成功");
    }

    @Override
    public CommonResponse<String> deleteticketbuyer(Map<String, Object> params) {
        Integer linkid =(Integer) params.get("linkId");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",linkid);
        Integer i = baseMapper.delete(queryWrapper);
        if (i>0){
            return ResponseUtil.returnSuccess("删除成功");
        }
        return ResponseUtil.returnFail("8888","删除失败");
    }

    @Override
    public LinkUser islinkUser(Long id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",id);

        return baseMapper.selectOne(queryWrapper);
    }
}
